package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.Users;
import com.dao.DeleteUser;
import com.dao.UpdateUser;
import com.dao.UserDao;
import com.util.ScreenUtils;
import com.util.SelectUsers;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class UpdateConsumerDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private String UserID;

    private JTextField IDField;
    //private JComboBox<String> typeComboBox;
    private JTextField nameField;
    private JTextField passwordField;
    private JComboBox<String> ConsumerGenderBox;
    private JTextField phoneField;
    private JTextField emailField;
    private JTextField addressField;

    private ActionDoneListener listener;

    public UpdateConsumerDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener,String UserID){
        super(jf,title,isModel);
        this.listener  = listener;
        this.UserID = UserID;

        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装用户编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("用户编号：");
        IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装用户类型
//        Box typeBox = Box.createHorizontalBox();
//        JLabel typeLabel = new JLabel("用户类型：");
//        String[] userTypes = {"消费者", "农户", "管理员"};
//        typeComboBox = new JComboBox<>(userTypes);
//        typeComboBox.setAlignmentX(Component.CENTER_ALIGNMENT);
//
//        typeBox.add(typeLabel);
//        typeBox.add(Box.createHorizontalStrut(15));
//        typeBox.add(typeComboBox);

        //组装用户名
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("用  户  名：");
        nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装登录密码
        Box passwordBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("登陆密码：");
        passwordField = new JTextField(15);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(15));
        passwordBox.add(passwordField);

        //组装性别
        Box genderBox = Box.createHorizontalBox();
        JLabel genderLabel = new JLabel("  性     别：");
        ConsumerGenderBox = new JComboBox<>();
        ConsumerGenderBox.addItem("男");
        ConsumerGenderBox.addItem("女");

        genderBox.add(genderLabel);
        genderBox.add(Box.createHorizontalStrut(20));
        genderBox.add(ConsumerGenderBox);

        //组装联系电话
        Box phoneBox = Box.createHorizontalBox();
        JLabel phoneLabel = new JLabel("联系电话：");
        phoneField = new JTextField(15);

        phoneBox.add(phoneLabel);
        phoneBox.add(Box.createHorizontalStrut(15));
        phoneBox.add(phoneField);

        //组装电子邮箱
        Box emailBox = Box.createHorizontalBox();
        JLabel emailLabel = new JLabel("电子邮箱：");
        emailField = new JTextField(15);

        emailBox.add(emailLabel);
        emailBox.add(Box.createHorizontalStrut(  15));
        emailBox.add(emailField);

        //组装家庭住址
        Box addressBox = Box.createHorizontalBox();
        JLabel addressLabel = new JLabel("家庭住址：");
        addressField = new JTextField(15);

        addressBox.add(addressLabel);
        addressBox.add(Box.createHorizontalStrut(15));
        addressBox.add(addressField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton updateBtn = new JButton("修改");
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户修改后在输入框中的内容
                int id = Integer.valueOf(IDField.getText().trim());
                //String UserType = (String) typeComboBox.getSelectedItem();
                String Username = nameField.getText().trim();
                String Password = passwordField.getText().trim();
                String Gender = (String) ConsumerGenderBox.getSelectedItem();
                String PhoneNumber = phoneField.getText().trim();
                String Email = emailField.getText().trim();
                String Address = addressField.getText().trim();

                //修改数据库中的数据
                if(Username.length()==0||Password.length()==0)
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，请添加用户名或密码！");
                }
                else if(Gender.length()==0||Email.length()==0||Address.length()==0){
                    JOptionPane.showMessageDialog(jf, "添加失败，请继续完善信息！");
                }
                else if(!Username.matches("\\w+"))
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，用户名只能包含英文数字下划线!");
                }
                else if(Password.length()<=8)
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，用户密码需要8位以上!");
                }
                else if(!PhoneNumber.matches("\\d{11,12}")){
                    JOptionPane.showMessageDialog(jf, "联系电话是11或12位数字组成!");
                }
                else{
                    Users user = new Users(id,"消费者",Username,Password,Gender,PhoneNumber,Email,Address);
                    new UpdateUser().updateUser(user,Integer.parseInt(UserID));
                    JOptionPane.showMessageDialog(jf, "修改成功!");
                    listener.done(null);
                    dispose(); // 关闭修改窗口
                }
            }
        });

        btnBox.add(updateBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
//        vBox.add(Box.createVerticalStrut(10));
//        vBox.add(typeBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(genderBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(phoneBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(emailBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(addressBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        requestData();

        this.add(hBox);
    }
    //请求数据
    public void requestData(){
        List<Users> list = new SelectUsers().getUserData();
        for(Users u:list)
        {
            if(Integer.toString(u.getUserId()).equals(UserID)){
                IDField.setText(UserID);
                //typeComboBox.setSelectedItem(u.getUserType());
                nameField.setText(u.getUserName());
                passwordField.setText(u.getPassword());
                ConsumerGenderBox.setSelectedItem(u.getGerder());
                phoneField.setText(u.getPhoneNumber());
                emailField.setText(u.getEmail());
                addressField.setText(u.getAddress());
                break;
            }
        }
    }

}
